package linkedlist;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

class ILinkedHashMap<K,V> extends LinkedHashMap{

    private  int MAX_ENTRIES = 100;

    @Override
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > MAX_ENTRIES;
    }



    public ILinkedHashMap(int capacity) {
        super(capacity, 0.75f, true);
        this.MAX_ENTRIES = capacity;

    }
}

public class LRUCache {

    ILinkedHashMap<Integer,Integer> iLinkedHashMap;



    public LRUCache(int cap){
        iLinkedHashMap = new ILinkedHashMap<Integer, Integer>(cap);
    }



    public int get(int key) {

        Integer value = (Integer) iLinkedHashMap.get(key);
        if (value == null) {
            return -1;
        } else {
            return value;
        }

    }

    public void put(int key, int value) {

        iLinkedHashMap.put(key, value);

    }

    public void list(){
        Set<Map.Entry<Integer,Integer>> resultSet =  iLinkedHashMap.entrySet();

        for (Map.Entry<Integer, Integer> entry : resultSet) {
            System.out.println("key is "+entry.getKey());
            System.out.println("value is "+ entry.getValue());
        }
    }




    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(16);
        for (int i = 0; i < 20; i++) {
            lruCache.put(i, i);
        }
        lruCache.get(10);
        lruCache.get(15);

        lruCache.list();




    }
}